package com.iplant.aps.serviceImpl;

import com.iplant.aps.service.po.aps.APSTaskPart;
import com.iplant.aps.service.po.aps.APSTaskPartPoint;
import com.iplant.aps.service.po.aps.APSTaskPartPointDay;
import com.iplant.aps.service.po.oms.OMSOrder;
import com.iplant.aps.serviceImpl.dao.APSTaskPartDAO;
import com.iplant.aps.serviceImpl.dao.aps.APSTaskPartPointDayDAO;
import com.iplant.aps.serviceImpl.dao.oms.OMSOrderDAO;
import com.iplant.aps.serviceImpl.utils.mrp.MRPDeliveryUtils;
import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.general.CalendarTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.orm.ORMUtils;
import com.iplant.base.utils.sql.orm.PoService;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.aps.service.MRPService;
import com.iplant.aps.service.po.mrp.MRPMaterialItem;
import com.iplant.aps.service.po.mrp.MRPMaterialPlan;
import com.iplant.aps.serviceImpl.dao.mrp.MRPMaterialItemDAO;
import com.iplant.aps.serviceImpl.dao.mrp.MRPMaterialPlanDAO;
import com.iplant.pdm.service.po.bop.FPCRoutePartPoint;
import com.iplant.pdm.service.po.mss.MSSBOMItem;
import com.iplant.pdm.serviceImpl.dao.mss.MSSBOMItemDAO;
import com.iplant.wms.service.po.mrp.MRPDelivery;
import com.iplant.wms.service.po.mrp.MRPDeliveryItem;
import com.iplant.wms.serviceImpl.dao.mrp.MRPDeliveryDAO;
import com.iplant.wms.serviceImpl.dao.mrp.MRPDeliveryItemDAO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class MRPServiceImpl implements MRPService {


    //region 构造函数
    public MRPServiceImpl() {
    }
    //endregion

    //region 单实例

    private static MRPService Instance = null;

    public static MRPService getInstance() {
        if (Instance == null)
            Instance = new MRPServiceImpl();

        return Instance;
    }

    //endregion

    //region POService

    PoService<APSTaskPartPointDay> mAPSTaskPartPointDayService = ORMUtils.GetPoService(APSTaskPartPointDay.class);
    PoService<FPCRoutePartPoint> mFPCRoutePartPointService = ORMUtils.GetPoService(FPCRoutePartPoint.class);
    PoService<APSTaskPartPoint> mAPSTaskPartPointService = ORMUtils.GetPoService(APSTaskPartPoint.class);
    PoService<APSTaskPart> mAPSTaskPartService = ORMUtils.GetPoService(APSTaskPart.class);
    //endregion

    /**
     * @param wLoginUser
     * @param wStartDate
     * @param wEndDate
     * @return
     */
    @Override
    public ServiceResult<List<MRPMaterialPlan>> MRP_CreateMaterialPlan(BMSEmployee wLoginUser, Calendar wStartDate, Calendar wEndDate) {
        ServiceResult<List<MRPMaterialPlan>> wResult = new ServiceResult<>();

        wResult.Result = new ArrayList<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            wStartDate = CalendarTool.CloneMonth(wStartDate, 1);
            wEndDate = CalendarTool.CloneMonth(wEndDate, 1);
            wEndDate.add(Calendar.MONTH, 1);

            List<APSTaskPart> wTaskPartList = APSTaskPartDAO.getInstance().APS_SelectTaskPartList(wLoginUser, wStartDate, wEndDate, wErrorCode);

            // region 检查输入条件合法性

            if (wTaskPartList == null)

                wTaskPartList = new ArrayList<>();

            Map<Integer, List<APSTaskPart>> wTaskPartListMap = wTaskPartList.stream().filter(p -> p.OrderID > 0).collect(Collectors.groupingBy(p -> p.OrderID));

            List<Integer> wOrderIDList = wTaskPartListMap.keySet().stream().distinct().collect(Collectors.toList());
            List<OMSOrder> wOMSOrderList;

            if (wOrderIDList == null || wOrderIDList.size() == 0) {
                wOMSOrderList = new ArrayList<>();
            } else {
                wOMSOrderList = OMSOrderDAO.getInstance().SelectPOList(wLoginUser, wOrderIDList, Pagination.getMaxSize(), wErrorCode);
                if (wErrorCode.Result != 0) {
                    wResult.AddFaultCode(wErrorCode);
                    return wResult;
                }
            }

            // endregion

            //  查询现有物料计划
            List<MRPMaterialPlan> wMaterialPlanList = MRPMaterialPlanDAO.getInstance()
                    .MRP_SelectMaterialPlanListByDate(wLoginUser, wStartDate, wEndDate, Pagination.getMaxSize(), wErrorCode);

            if (wErrorCode.Result != 0) {
                wResult.AddFaultCode(wErrorCode);
                return wResult;
            }

            Map<Integer, List<MRPMaterialPlan>> wOrderPlanListMap
                    = wMaterialPlanList.stream().filter(p -> p.OrderID > 0).collect(Collectors.groupingBy(p -> p.OrderID));

            Map<Integer, List<MSSBOMItem>> wBOMItemListMap = new HashMap<>();
            List<MSSBOMItem> wRouteBOMItemList;
            //  MSSMaterial wMSSMaterial;
            List<APSTaskPart> wOrderTaskPartList;
            MRPMaterialPlan wMRPMaterialPlan;
            List<MRPMaterialPlan> wOrderPlanList;

            Map<String, Map<Integer, List<MRPMaterialPlan>>> wOrderMaterialPlanListMap;

            Calendar wDemandDate;
            MRPMaterialItem wMaterialItem;

            Map<String, Map<Integer, List<MRPMaterialItem>>> wMaterialItemListMap;

            String wDemandMonthString = "";

            List<MRPMaterialPlan> wDeleteMaterialPlanList = new ArrayList<>();


            for (OMSOrder wOMSOrder : wOMSOrderList) {


                if (!wBOMItemListMap.containsKey(wOMSOrder.RouteID)) {
                    wBOMItemListMap.put(wOMSOrder.RouteID,
                            MSSBOMItemDAO.getInstance().MSS_SelectBOMItemByRouteID(wLoginUser, wOMSOrder.RouteID, Pagination.getNewMaxSize(), wErrorCode));
                    if (wErrorCode.Result != 0) {
                        wResult.AddFaultCode(wErrorCode);
                        return wResult;
                    }
                }
                wRouteBOMItemList = wBOMItemListMap.get(wOMSOrder.RouteID);
                if (!wTaskPartListMap.containsKey(wOMSOrder.ID)) {
                    wResult.FaultCode += "订单对应的计划不存在！";
                    return wResult;
                }

                wOrderTaskPartList = wTaskPartListMap.get(wOMSOrder.ID);

                if (!wOrderPlanListMap.containsKey(wOMSOrder.ID)) {
                    wOrderPlanListMap.put(wOMSOrder.ID, new ArrayList<>());
                }
                wOrderPlanList = wOrderPlanListMap.get(wOMSOrder.ID);

                wOrderMaterialPlanListMap = wOrderPlanList.stream().collect(Collectors.groupingBy(q -> StringUtils.parseCalendarToString(q.DemandMonth, "yyyy-MM-dd"),
                        Collectors.groupingBy(f -> f.MaterialID)));

                wMaterialItemListMap = new HashMap<>();
                for (APSTaskPart apsTaskPart : wOrderTaskPartList) {
                    if (apsTaskPart.TaskPartPointList != null && apsTaskPart.TaskPartPointList.size() > 0) {

                        for (APSTaskPartPoint apsTaskPartPoint : apsTaskPart.TaskPartPointList) {

                            for (MSSBOMItem mssbomItem : wRouteBOMItemList) {
                                if (mssbomItem.RoutePartID != apsTaskPartPoint.RoutePartID
                                        || mssbomItem.RoutePartPointID != apsTaskPartPoint.RoutePartPointID)
                                    continue;

                                for (String wDateString : apsTaskPartPoint.PlanDayNum.keySet()) {

                                    wDemandDate = StringUtils.parseCalendar(wDateString);

                                    if (wDemandDate.compareTo(wStartDate) <= 0 || wDemandDate.compareTo(wEndDate) >= 0) {
                                        continue;
                                    }
                                    if (apsTaskPartPoint.PlanDayNum.get(wDateString) <= 0)
                                        continue;

                                    wDemandMonthString = StringUtils.parseCalendarToString(wDemandDate, "yyyy-MM-01");

                                    wMaterialItem = new MRPMaterialItem(wOMSOrder, wDemandDate, apsTaskPartPoint, mssbomItem, apsTaskPartPoint.PlanDayNum.get(wDateString));
                                    if (!wMaterialItemListMap.containsKey(wDemandMonthString)) {
                                        wMaterialItemListMap.put(wDemandMonthString, new HashMap<>());
                                    }
                                    if (!wMaterialItemListMap.get(wDemandMonthString).containsKey(mssbomItem.MaterialID)) {
                                        wMaterialItemListMap.get(wDemandMonthString).put(mssbomItem.MaterialID, new ArrayList<>());
                                    }
                                    wMaterialItemListMap.get(wDemandMonthString).get(mssbomItem.MaterialID).add(wMaterialItem);
                                }
                            }

                        }
                    } else {
                        for (MSSBOMItem mssbomItem : wRouteBOMItemList) {
                            if (mssbomItem.RoutePartID != apsTaskPart.RoutePartID)
                                continue;
                            for (String wDateString : apsTaskPart.PlanDayNum.keySet()) {

                                wDemandDate = StringUtils.parseCalendar(wDateString);

                                if (wDemandDate.compareTo(wStartDate) <= 0 || wDemandDate.compareTo(wEndDate) >= 0) {
                                    continue;
                                }
                                if (apsTaskPart.PlanDayNum.get(wDateString) <= 0)
                                    continue;

                                wMaterialItem = new MRPMaterialItem(wOMSOrder, wDemandDate, apsTaskPart, mssbomItem, apsTaskPart.PlanDayNum.get(wDateString));

                                wDemandMonthString = StringUtils.parseCalendarToString(wDemandDate, "yyyy-MM-01");
                                if (!wMaterialItemListMap.containsKey(wDemandMonthString)) {
                                    wMaterialItemListMap.put(wDemandMonthString, new HashMap<>());
                                }
                                if (!wMaterialItemListMap.get(wDemandMonthString).containsKey(mssbomItem.MaterialID)) {
                                    wMaterialItemListMap.get(wDemandMonthString).put(mssbomItem.MaterialID, new ArrayList<>());
                                }
                                wMaterialItemListMap.get(wDemandMonthString).get(mssbomItem.MaterialID).add(wMaterialItem);
                            }
                        }
                    }
                }

                //根据子项生成父级计划
                for (String wMonthString : wMaterialItemListMap.keySet()) {
                    Calendar wDemandMonth = StringUtils.parseCalendar(wMonthString);

                    if (!wOrderMaterialPlanListMap.containsKey(wMonthString))
                        wOrderMaterialPlanListMap.put(wMonthString, new HashMap<>());
                    for (Integer wMaterialID : wMaterialItemListMap.get(wMonthString).keySet()) {

                        if (!wOrderMaterialPlanListMap.get(wMonthString).containsKey(wMaterialID)) {
                            wOrderMaterialPlanListMap.get(wMonthString).put(wMaterialID, new ArrayList<>());
                            wMRPMaterialPlan = new MRPMaterialPlan(wOMSOrder, wDemandMonth, wMaterialItemListMap.get(wMonthString).get(wMaterialID));

                            wOrderMaterialPlanListMap.get(wMonthString).get(wMaterialID).add(wMRPMaterialPlan);
                            //没有 创建计划
                        } else {
                            wMRPMaterialPlan = wOrderMaterialPlanListMap.get(wMonthString).get(wMaterialID).get(0);

                            //所有子项删除
                            wMRPMaterialPlan.setMaterialPlanItem(wMaterialItemListMap.get(wMonthString).get(wMaterialID));
                            //将数量根据子项修改
                        }
                        MRPMaterialPlanDAO.getInstance().MRP_UpdateMaterialPlan(wLoginUser, wMRPMaterialPlan, wErrorCode);

                        if (wErrorCode.Result != 0) {
                            wResult.AddFaultCode(wErrorCode);
                            return wResult;
                        }
                        wResult.Result.add(wMRPMaterialPlan);

                    }
                }

            }


            List<Integer> wDeletePlanIDList = new ArrayList<>();
            List<Integer> wDeleteItemIDList = new ArrayList<>();
            boolean wIsOwn = false;
            for (MRPMaterialPlan mrpMaterialPlan : wMaterialPlanList) {
                wIsOwn = false;
                for (MRPMaterialPlan materialPlan : wResult.Result) {
                    if (materialPlan.ID != mrpMaterialPlan.ID)
                        continue;
                    if (mrpMaterialPlan.ItemList == null || mrpMaterialPlan.ItemList.size() == 0)
                        continue;

                    for (MRPMaterialItem mrpMaterialItem : mrpMaterialPlan.ItemList) {
                        wIsOwn = false;
                        for (MRPMaterialItem materialItem : materialPlan.ItemList) {
                            if (mrpMaterialItem.ID != materialItem.ID)
                                continue;
                            wIsOwn = true;
                        }
                        if (!wIsOwn) {
                            wDeleteItemIDList.add(mrpMaterialItem.ID);
                        }
                    }
                    wIsOwn = true;
                }
                if (!wIsOwn) {
                    wDeletePlanIDList.add(mrpMaterialPlan.ID);
                }
            }
            //删除 自身 删除子项
            MRPMaterialPlanDAO.getInstance().MRP_DeleteMaterialPlanByIDList(wLoginUser, wDeletePlanIDList, wErrorCode);

            if (wErrorCode.Result != 0) {
                wResult.AddFaultCode(wErrorCode);
                return wResult;
            }
            MRPMaterialItemDAO.getInstance().MRP_DeleteMaterialItemByIDList(wLoginUser, wDeleteItemIDList, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            log.error("MRP_CreateMaterialPlan", e);
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<MRPMaterialPlan>> MRP_SelectMaterialPlanListByDate(BMSEmployee wLoginUser,
                                                                                 Calendar wStartDate, Calendar wEndDate, Pagination wPagination) {
        ServiceResult<List<MRPMaterialPlan>> wResult = new ServiceResult<>();

        wResult.Result = new ArrayList<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);


            //  查询现有物料计划
            wResult.Result = MRPMaterialPlanDAO.getInstance()
                    .MRP_SelectMaterialPlanListByDate(wLoginUser, wStartDate, wEndDate, Pagination.getMaxSize(), wErrorCode);


            wResult.AddFaultCode(wErrorCode);
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            log.error("MRP_CreateMaterialPlan", e);
        }
        return wResult;
    }


    @Override
    public ServiceResult<List<MRPDelivery>> MRP_CreateDelivery(BMSEmployee wLoginUser, Calendar wStartDate, Calendar wEndDate) {
        ServiceResult<List<MRPDelivery>> wResult = new ServiceResult<>();

        wResult.Result = new ArrayList<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            //  查询现有物料计划
            List<MRPDelivery> wMRPDeliveryList = MRPDeliveryDAO.getInstance()
                    .MRP_SelectDeliveryListByDate(wLoginUser, wStartDate, wEndDate, Pagination.getNewMaxSize(), wErrorCode);
            if (wErrorCode.Result != 0) {
                wResult.AddFaultCode(wErrorCode);
                return wResult;
            }

            //获取已下达的日计划
            Map<String, Object> wPartPointDayParams = new HashMap<>();

            wPartPointDayParams.put("StationDateStartTime", wStartDate);
            wPartPointDayParams.put("StationDateEndTime", wEndDate);

            List<APSTaskPartPointDay> wTaskPartList = APSTaskPartPointDayDAO.getInstance()
                    .SelectPOList(wLoginUser, wPartPointDayParams, Pagination.getNewMaxSize(), wErrorCode);

            // region 检查输入条件合法性
            if (wTaskPartList == null || wTaskPartList.size() == 0) {
                if (wMRPDeliveryList.size() > 0) {

                    MRPDeliveryDAO.getInstance().MRP_DeleteDeliveryList(wLoginUser, wMRPDeliveryList, wErrorCode);
                }
                return wResult;
            }

            Map<Integer, List<APSTaskPartPointDay>> wTaskPartListMap = wTaskPartList.stream().filter(p -> p.OrderID > 0).collect(Collectors.groupingBy(p -> p.OrderID));

            List<Integer> wOrderIDList = wTaskPartListMap.keySet().stream().distinct().collect(Collectors.toList());
            List<OMSOrder> wOMSOrderList;

            if (wOrderIDList == null || wOrderIDList.size() == 0) {
                wResult.FaultCode += "计划中的订单ID不能为空";
                return wResult;
            }
            wOMSOrderList = OMSOrderDAO.getInstance().SelectPOList(wLoginUser, wOrderIDList, Pagination.getMaxSize(), wErrorCode);
            if (wErrorCode.Result != 0) {
                wResult.AddFaultCode(wErrorCode);
                return wResult;
            }
            if (wOMSOrderList == null || wOMSOrderList.size() == 0) {
                wResult.FaultCode += "计划中的订单不存在！";
                return wResult;
            }
            // endregion

            Map<Integer, List<MRPDelivery>> wOrderPlanListMap
                    = wMRPDeliveryList.stream().filter(p -> p.OrderID > 0).collect(Collectors.groupingBy(p -> p.OrderID));

            Map<Integer, Map<Integer, List<MSSBOMItem>>> wBOMItemListMap = new HashMap<>();
            List<MSSBOMItem> wRouteBOMItemList;

            Map<Integer, List<MSSBOMItem>> wRouteBOMItemMap;
            //  MSSMaterial wMSSMaterial;
            List<APSTaskPartPointDay> wOrderTaskPartList;

            Map<Integer, Map<String, List<APSTaskPartPointDay>>> wOrderTaskPartListMap;
            MRPDelivery wMRPDelivery;
            List<MRPDelivery> wOrderPlanList;

            Map<String, Map<Integer, List<MRPDelivery>>> wOrderDeliveryListMap;

            Calendar wDemandDate;
            MRPDeliveryItem wDeliveryItem;

            Map<String, Map<Integer, List<MRPDeliveryItem>>> wDeliveryItemListMap;


            for (OMSOrder wOMSOrder : wOMSOrderList) {

                if (!wBOMItemListMap.containsKey(wOMSOrder.RouteID)) {
                    wRouteBOMItemList = MSSBOMItemDAO.getInstance().MSS_SelectBOMItemByRouteID(wLoginUser, wOMSOrder.RouteID, Pagination.getNewMaxSize(), wErrorCode);
                    if (wErrorCode.Result != 0) {
                        wResult.AddFaultCode(wErrorCode);
                        return wResult;
                    }
                    wBOMItemListMap.put(wOMSOrder.RouteID,
                            wRouteBOMItemList.stream().collect(Collectors.groupingBy(p -> p.RoutePartPointID)));
                }
                wRouteBOMItemMap = wBOMItemListMap.get(wOMSOrder.RouteID);
                if (!wTaskPartListMap.containsKey(wOMSOrder.ID)) {
                    wResult.FaultCode += "订单对应的计划不存在！";
                    return wResult;
                }

                wOrderTaskPartList = wTaskPartListMap.get(wOMSOrder.ID);

                wOrderTaskPartListMap = wOrderTaskPartList.stream().collect(Collectors.groupingBy(p -> p.RoutePartPointID,
                        Collectors.groupingBy(q -> StringUtils.parseCalendarToString(q.StationDate, "yyyy-MM-dd"))));

                if (!wOrderPlanListMap.containsKey(wOMSOrder.ID)) {
                    wOrderPlanListMap.put(wOMSOrder.ID, new ArrayList<>());
                }
                wOrderPlanList = wOrderPlanListMap.get(wOMSOrder.ID);

                wOrderDeliveryListMap = wOrderPlanList.stream().collect(Collectors.groupingBy(q -> StringUtils.parseCalendarToString(q.DemandDate, "yyyy-MM-dd"),
                        Collectors.groupingBy(f -> f.MaterialID)));

                wDeliveryItemListMap = new HashMap<>();
                for (Integer wRoutePartPointID : wOrderTaskPartListMap.keySet()) {

                    if (!wRouteBOMItemMap.containsKey(wRoutePartPointID)) {
                        continue;
                    }
                    for (String wDemandDateString : wOrderTaskPartListMap.get(wRoutePartPointID).keySet()) {

                        for (MSSBOMItem mssbomItem : wRouteBOMItemMap.get(wRoutePartPointID)) {

                            wDeliveryItem = MRPDeliveryUtils.getInstance().MRP_CreateDeliveryItem(wOMSOrder,
                                    wOrderTaskPartListMap.get(wRoutePartPointID).get(wDemandDateString), mssbomItem);

                            if (wDeliveryItem.OrderPlan <= 0)
                                continue;

                            if (!wDeliveryItemListMap.containsKey(wDemandDateString)) {
                                wDeliveryItemListMap.put(wDemandDateString, new HashMap<>());
                            }
                            if (!wDeliveryItemListMap.get(wDemandDateString).containsKey(mssbomItem.MaterialID)) {
                                wDeliveryItemListMap.get(wDemandDateString).put(mssbomItem.MaterialID, new ArrayList<>());
                            }
                            wDeliveryItemListMap.get(wDemandDateString).get(mssbomItem.MaterialID).add(wDeliveryItem);
                        }
                    }
                }

                //根据子项生成父级计划
                for (String wMonthString : wDeliveryItemListMap.keySet()) {
                    Calendar wDemandMonth = StringUtils.parseCalendar(wMonthString);

                    if (!wOrderDeliveryListMap.containsKey(wMonthString))
                        wOrderDeliveryListMap.put(wMonthString, new HashMap<>());
                    for (Integer wMaterialID : wDeliveryItemListMap.get(wMonthString).keySet()) {

                        if (!wOrderDeliveryListMap.get(wMonthString).containsKey(wMaterialID)) {
                            wOrderDeliveryListMap.get(wMonthString).put(wMaterialID, new ArrayList<>());
                            wMRPDelivery = MRPDeliveryUtils.getInstance().MRP_CreateDelivery(wOMSOrder, wDeliveryItemListMap.get(wMonthString).get(wMaterialID));

                            wOrderDeliveryListMap.get(wMonthString).get(wMaterialID).add(wMRPDelivery);
                            //没有 创建计划
                        } else {
                            wMRPDelivery = wOrderDeliveryListMap.get(wMonthString).get(wMaterialID).get(0);

                            //所有子项删除
                            wMRPDelivery.setDeliveryItem(wDeliveryItemListMap.get(wMonthString).get(wMaterialID));
                            //将数量根据子项修改
                        }
                        MRPDeliveryDAO.getInstance().MRP_UpdateDelivery(wLoginUser, wMRPDelivery, wErrorCode);

                        if (wErrorCode.Result != 0) {
                            wResult.AddFaultCode(wErrorCode);
                            return wResult;
                        }
                        wResult.Result.add(wMRPDelivery);

                    }
                }
            }


            //两个列表对比，多的标记Active=2; 少的不管 todo
            List<Integer> wDeletePlanIDList = new ArrayList<>();

            List<Integer> wDeleteItemIDList = new ArrayList<>();
            boolean wIsOwn = false;
            for (MRPDelivery mrpDelivery : wMRPDeliveryList) {
                wIsOwn = false;
                for (MRPDelivery delivery : wResult.Result) {
                    if (delivery.ID != mrpDelivery.ID)
                        continue;
                    if (mrpDelivery.ItemList == null || mrpDelivery.ItemList.size() == 0)
                        continue;

                    for (MRPDeliveryItem mrpDeliveryItem : mrpDelivery.ItemList) {
                        wIsOwn = false;
                        for (MRPDeliveryItem deliveryItem : delivery.ItemList) {
                            if (mrpDeliveryItem.ID != deliveryItem.ID)
                                continue;
                            wIsOwn = true;
                        }
                        if (!wIsOwn) {
                            wDeleteItemIDList.add(mrpDeliveryItem.ID);
                        }
                    }
                    wIsOwn = true;
                }
                if (!wIsOwn) {
                    wDeletePlanIDList.add(mrpDelivery.ID);
                    if (mrpDelivery.ItemList != null && mrpDelivery.ItemList.size() > 0)
                        wDeleteItemIDList.addAll(mrpDelivery.ItemList.stream().map(p -> p.ID).collect(Collectors.toList()));
                }
            }

            //删除 自身 删除子项
            MRPDeliveryDAO.getInstance().MRP_DeleteDeliveryByIDList(wLoginUser, wDeletePlanIDList, wErrorCode);

            if (wErrorCode.Result != 0) {
                wResult.AddFaultCode(wErrorCode);
                return wResult;
            }

            MRPDeliveryItemDAO.getInstance().MRP_DeleteDeliveryItemByIDList(wLoginUser, wDeleteItemIDList, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();

            log.error("MRP_CreateDelivery", e);
        }
        return wResult;
    }


}
